<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/9
 * Time: 23:14
 */

namespace app\index\controller;


use app\model\Role;

class Authority
{
    /**
     * 添加角色
     * @return false|string
     */
    public function addRole(){
        if(request()->isPost()){
            $data = request()->only('role_name');
            if(isset($data["role_name"])){
                $role = new Role();
                if($role->check($data["role_name"]) == 0) return res(0,'该角色名字已存在');
                if($role->add($data)){
                    return res(1,'添加成功');
                }
                return res(0,'添加失败');
            }
            return res(0,'参数不完整');
        }
        return res(0,'请求方式错误');
    }

    /**
     * 删除角色
     * @param $id
     * @return false|string
     */
    public function delRole($id){
        $role = new Role();
        if($role->del($id)){
            return res(1,'删除成功');
        }
        return res(0,'删除失败');
    }

    /**
     * 修改角色
     * @return false|string
     */
    public function editRole(){
        if(request()->isPost()){
            $data = request()->only('role_id,role_name');
            if (isset($data["role_id"]) && isset($data["role_name"])){
                $id = $data['role_id'];
                unset($data['role_id']);
                $role = new Role();
                if($role->edit($id,$data)){
                    return res(1,'修改成功');
                }
                return res(0,'删除失败');
            }
            return res(0,'参数不完整');
        }
        return res(0,'请求方式错误');
    }

}